<?php
class Mysql
{

    //成员数据：
    protected $dbName;
    protected $tables;
    protected $dbUser; //数据库用户名
    protected $dbPassword; //数据库密码
    protected $affectedRows;
    protected $server;
    protected $link;
    protected $result;


    //成员函数的定义：
    public function __construct($srv,$user,$pw,$db="")
    { //构造函数：
        $this->server = $srv;
        $this->dbUser = $user;
        $this->dbPassword = $pw;
        $this->link = mysql_connect($srv,$user,$pw);
        if($db !="")
             $this->dbName = $db;
        if(!mysql_select_db($db,$this->link))
        {
            mysql_select_db($db,$this->link);
        }
        echo mysql_error();

        $this->charset("utf8");
    }


    public function __destruct()
    {
        $this->close();
    }

    public function close()
    {//关闭数据库连接：
        if(is_resource($this->result))
            $this->freeResult();
        mysql_close($this->link);
    }

    public function useDb($db)
    {//选择给定的数据库：
        return mysql_select_db($db,$this->link);
    }

    public function charset($set)
    {
        $this->query("SET NAMES $set");
    }

    public function freeResult()
    { //释放查询语句的结果资源：
        if(is_resource($this->result))
            mysql_free_result($this->result);
    }

    public function query($sql)
    {//直接执行 SQL 语句：
         $this->result = mysql_query($sql,$this->link);
         return $this->result;
    }

    //把某一列的若干记录读入数组中.
    public function getColRows($sql)
    {
         $this->query($sql);
         $num = mysql_num_rows($this->result);
         if($num==0)
             return false;
         $arr = array($num);//用于接受记录集的数组。
         $i = 0;
         while($row = mysql_fetch_array($this->result))
         {
               $arr[$i++] = $row[0];
         }
         return $arr;
    }

    //读取若条记录,保存在一个数组中返回,每条记录可有若干列:
    public function getRows($sql)
    {
        $this->result = mysql_query($sql);
        $num = mysql_num_rows($this->result);
        if($num==0)
           return false;

        $arr = array($num);
        $i=0;
        while($row = mysql_fetch_assoc($this->result))
        {
            $arr[$i++] = $row;
        }
        return $arr;
    }

    //返回sql起作用的语句数。
    public function affectedRows()
    {
        return mysql_num_rows($this->result);
    }

    //根据限制删除记录,返回boolean值:
    public function del($table,$limit)
    {
        return $this->query("DELETE FROM $table WHERE $limit");
    }

    public function dropTable($table)
    {//删除给出的表：
        return mysql_query("DROP TABLE $table");
        return false;
    }

    public function count($table,$limit)
    {
        $n = $this->getColRows("SELECT COUNT(*) FROM $table WHERE $limit");
        return $n[0];
    }


    public function save($table, $data)
    {
        $r = true;
        if(!isset($data['created']) && $this->colExists($table, 'created')){
            //自动保存时间
            $data['created'] = time();
        }
        $cols = "";
        foreach($data as $col=>$val){
            if(!$this->colExists($table, $col)){
                $data[$col] = null;
                unset($data[$col]);
            }
        }
        $cols = join(",", array_keys($data));
        $vals = "'".join("','", array_values($data))."'";
        $sql = "INSERT INTO $table($cols) VALUES($vals)";
        $r = $r && $this->query($sql);

        return $r;
    }

    public function viewIncrease($table, $where)
    {
        $this->query("UPDATE $table SET viewed=viewed+1 WHERE $where");
    }


    public function getColType($table, $col)
    {
        $this->query("SELECT $col FROM $table");
        return mysql_field_type($this->result,0);
    }

    public function getColLen($table, $col)
    {
        $this->query("SELECT $col FROM $table ");
        return mysql_field_len($this->result, 0);
    }


    public function colExists($table, $col)
    {
        $arr = $this->getColRows("DESCRIBE $table");
        foreach($arr as $item)
        {
            if($col == $item)
                return true;
        }
        return FALSE;
    }

    public function dropTables($prefix="")
    {
        $tables = $this->query("SHOW TABLES");

        $i = 0;
        while($tb = mysql_fetch_array($tables))
        {
            $tb = $tb[0];
            if(!empty($prefix) && stripos($tb, $prefix)!==false && stripos($tb, $prefix)==0){
                $this->query("DROP TABLE $tb");
                echo "已经删除 $tb <br />";
                $i ++;
            }
        }

        echo $i . " 已经删除 $i 个表格<br />";

    }

    public function getPrimaryKey($table)
    {
        $rows = $this->getRows("DESCRIBE $table");
        foreach($rows as $row){
            if(strtolower($row['Key']) == "pri")
                return $row['Field'];
        }

        return null;
    }

    public function getColDefalt($table, $col)
    {
        $rows = $this->getRows("DESCRIBE $table");
        foreach($rows as $row){
            if(strtolower($row['Field']) == strtolower($col))
                return $row['Default'];
        }

        return null;
    }

    public function getColNames($table)
    {
        return $this->getColRows("DESCRIBE $table");
    }

    public function tableExists($table_name)
    {
        $table_name = strtolower($table_name);
        $result = $this->query("SHOW TABLES");
        while($tb = mysql_fetch_array($result))
        {
            if($table_name == strtolower($table_name))
                return true;
        }
        return false;
    }
}



header("content-type:text/html;charset=utf-8");
$mysql = new Mysql("localhost", "ljxok", "20071217kiss", "ljxok");
$mysql->dropTables("p8_");


function showInTable($settings, $data)
{
    $out = '';
    $keys = array();
    foreach($settings as $key => $capture){
        $out .= "<td>$capture</td>";
        $keys[] = $key;
    }

    $out = "<tr class='tableHeader'>$out</tr>";
    foreach($data as $i => $row){
       $out .= "<tr>";
       foreach($keys as $col){
           $out .= "<td class='$col'>{$row[$col]}</td>";
       }
       $out .= "</tr>";
    }
    $out = "<table>$out</table><style type='text/css'>table, tr, td {border:1px solid #eee}</style>";

    return $out;
}

